package com.abel.bigwater.dataserver.model.kming

import com.abel.bigwater.data.BcdString
import io.netty.buffer.ByteBuf
import java.util.*

class KmingRepondShutdown : TermData(KmingDataType.TermShutdownComm, 8) {

    var startTimeStr: BcdString? = null

    /**
     * 时间对象
     */
    var startTime: Date?
        get() = if (startTimeStr == null) null else startTimeStr!!.toFullTime()
        set(value) {
            startTimeStr = if (value == null) null
            else TermData.fromFullTime(value)
        }

    var sessionReponseType: SessionReponseType? = null

    override fun bufLength() = 8

    override fun parseBuf(buf: ByteBuf): Boolean {
        assert(type == KmingDataType.findByCode(buf.readUnsignedByte()))

        startTimeStr = BcdString.create(buf, 6)

        sessionReponseType = SessionReponseType.findByCode(buf.readByte())

        return true
    }

    override fun pack(buf: ByteBuf) {
        buf.apply {
            writeByte(type.code.toInt())

            writeBytes(startTimeStr!!.hexArray)

            writeByte(sessionReponseType!!.code.toInt())

            buf.writeBytes(this)
        }
    }
}